{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.append(os.path.abspath(os.path.join('..')))\n",
    "from ch07_autograd.utils import Scalar, draw_graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def scalar_sum(nums):\n",
    "    \"\"\"\n",
    "    为了作图美观，定义基于Scalar的求和运算\n",
    "    \"\"\"\n",
    "    value = 0.0\n",
    "    requires_grad = False\n",
    "    wrt = {}\n",
    "    for item in nums:\n",
    "        value += item.value\n",
    "        wrt[item] = 1.0\n",
    "        requires_grad = requires_grad or item.requires_grad\n",
    "    output = Scalar(value, nums, 'sum')\n",
    "    output.requires_grad=requires_grad\n",
    "    output.grad_wrt = wrt\n",
    "    return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"466pt\" height=\"336pt\"\n",
       " viewBox=\"0.00 0.00 466.46 336.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 332)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-332 462.462,-332 462.462,4 -4,4\"/>\n",
       "<!-- 4463805968forward -->\n",
       "<g id=\"node1\" class=\"node\"><title>4463805968forward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"29.8142\" cy=\"-27.5\" rx=\"29.6295\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"29.8142\" y=\"-24.5\" font-family=\"Times,serif\" font-size=\"10.00\">x1= 1.10</text>\n",
       "</g>\n",
       "<!-- 4463806160forward -->\n",
       "<g id=\"node4\" class=\"node\"><title>4463806160forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M86.1667,-91.5C86.1667,-91.5 125.462,-91.5 125.462,-91.5 131.462,-91.5 137.462,-97.5 137.462,-103.5 137.462,-103.5 137.462,-133.5 137.462,-133.5 137.462,-139.5 131.462,-145.5 125.462,-145.5 125.462,-145.5 86.1667,-145.5 86.1667,-145.5 80.1667,-145.5 74.1667,-139.5 74.1667,-133.5 74.1667,-133.5 74.1667,-103.5 74.1667,-103.5 74.1667,-97.5 80.1667,-91.5 86.1667,-91.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"105.814\" y=\"-133.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"74.1667,-127.5 137.462,-127.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"105.814\" y=\"-115.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 1.10</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"74.1667,-109.5 137.462,-109.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"105.814\" y=\"-97.5\" font-family=\"Times,serif\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 4463805968forward&#45;&gt;4463806160forward -->\n",
       "<g id=\"edge10\" class=\"edge\"><title>4463805968forward&#45;&gt;4463806160forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M43.0524,-44.0026C52.3667,-54.9102 65.1967,-69.9348 76.8302,-83.5583\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"74.3505,-86.0441 83.506,-91.3759 79.6737,-81.4984 74.3505,-86.0441\"/>\n",
       "</g>\n",
       "<!-- 4463806016forward -->\n",
       "<g id=\"node2\" class=\"node\"><title>4463806016forward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"189.814\" cy=\"-27.5\" rx=\"29.6295\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"189.814\" y=\"-24.5\" font-family=\"Times,serif\" font-size=\"10.00\">x2= 2.10</text>\n",
       "</g>\n",
       "<!-- 4463806304forward -->\n",
       "<g id=\"node8\" class=\"node\"><title>4463806304forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M248.167,-91.5C248.167,-91.5 287.462,-91.5 287.462,-91.5 293.462,-91.5 299.462,-97.5 299.462,-103.5 299.462,-103.5 299.462,-133.5 299.462,-133.5 299.462,-139.5 293.462,-145.5 287.462,-145.5 287.462,-145.5 248.167,-145.5 248.167,-145.5 242.167,-145.5 236.167,-139.5 236.167,-133.5 236.167,-133.5 236.167,-103.5 236.167,-103.5 236.167,-97.5 242.167,-91.5 248.167,-91.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"267.814\" y=\"-133.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"236.167,-127.5 299.462,-127.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"267.814\" y=\"-115.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 4.20</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"236.167,-109.5 299.462,-109.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"267.814\" y=\"-97.5\" font-family=\"Times,serif\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 4463806016forward&#45;&gt;4463806304forward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>4463806016forward&#45;&gt;4463806304forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M203.401,-44.0026C212.96,-54.9102 226.128,-69.9348 238.067,-83.5583\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"235.696,-86.1623 244.919,-91.3759 240.96,-81.5485 235.696,-86.1623\"/>\n",
       "</g>\n",
       "<!-- 4463806064forward -->\n",
       "<g id=\"node3\" class=\"node\"><title>4463806064forward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"347.814\" cy=\"-27.5\" rx=\"29.6295\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"347.814\" y=\"-24.5\" font-family=\"Times,serif\" font-size=\"10.00\">x3= 3.10</text>\n",
       "</g>\n",
       "<!-- 4463806400forward -->\n",
       "<g id=\"node10\" class=\"node\"><title>4463806400forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M329.167,-91.5C329.167,-91.5 368.462,-91.5 368.462,-91.5 374.462,-91.5 380.462,-97.5 380.462,-103.5 380.462,-103.5 380.462,-133.5 380.462,-133.5 380.462,-139.5 374.462,-145.5 368.462,-145.5 368.462,-145.5 329.167,-145.5 329.167,-145.5 323.167,-145.5 317.167,-139.5 317.167,-133.5 317.167,-133.5 317.167,-103.5 317.167,-103.5 317.167,-97.5 323.167,-91.5 329.167,-91.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"348.814\" y=\"-133.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"317.167,-127.5 380.462,-127.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"348.814\" y=\"-115.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 9.30</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"317.167,-109.5 380.462,-109.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"348.814\" y=\"-97.5\" font-family=\"Times,serif\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 4463806064forward&#45;&gt;4463806400forward -->\n",
       "<g id=\"edge9\" class=\"edge\"><title>4463806064forward&#45;&gt;4463806400forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M348.007,-45.6601C348.12,-55.7626 348.268,-68.9344 348.407,-81.2361\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"344.907,-81.3184 348.52,-91.2784 351.907,-81.2397 344.907,-81.3184\"/>\n",
       "</g>\n",
       "<!-- 4463806448forward -->\n",
       "<g id=\"node12\" class=\"node\"><title>4463806448forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M204.667,-182.5C204.667,-182.5 248.962,-182.5 248.962,-182.5 254.962,-182.5 260.962,-188.5 260.962,-194.5 260.962,-194.5 260.962,-224.5 260.962,-224.5 260.962,-230.5 254.962,-236.5 248.962,-236.5 248.962,-236.5 204.667,-236.5 204.667,-236.5 198.667,-236.5 192.667,-230.5 192.667,-224.5 192.667,-224.5 192.667,-194.5 192.667,-194.5 192.667,-188.5 198.667,-182.5 204.667,-182.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"226.814\" y=\"-224.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"192.667,-218.5 260.962,-218.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.814\" y=\"-206.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 18.60</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"192.667,-200.5 260.962,-200.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.814\" y=\"-188.5\" font-family=\"Times,serif\" font-size=\"10.00\">sum</text>\n",
       "</g>\n",
       "<!-- 4463806160forward&#45;&gt;4463806448forward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>4463806160forward&#45;&gt;4463806448forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M137.605,-142.884C151.785,-153.313 168.702,-165.757 183.974,-176.99\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"182.132,-179.98 192.262,-183.086 186.28,-174.341 182.132,-179.98\"/>\n",
       "</g>\n",
       "<!-- 4463805680forward -->\n",
       "<g id=\"node5\" class=\"node\"><title>4463805680forward</title>\n",
       "<polygon fill=\"springgreen\" stroke=\"black\" stroke-width=\"2\" points=\"237.167,-0.5 237.167,-54.5 300.462,-54.5 300.462,-0.5 237.167,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"268.814\" y=\"-42.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"237.167,-36.5 300.462,-36.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"268.814\" y=\"-24.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 2.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"237.167,-18.5 300.462,-18.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"268.814\" y=\"-6.5\" font-family=\"Times,serif\" font-size=\"10.00\">w2</text>\n",
       "</g>\n",
       "<!-- 4463805680forward&#45;&gt;4463806304forward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>4463805680forward&#45;&gt;4463806304forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M268.52,-54.7216C268.426,-63.0505 268.321,-72.4135 268.221,-81.3161\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"264.719,-81.4271 268.107,-91.4659 271.719,-81.5058 264.719,-81.4271\"/>\n",
       "</g>\n",
       "<!-- 4463805728forward -->\n",
       "<g id=\"node6\" class=\"node\"><title>4463805728forward</title>\n",
       "<polygon fill=\"springgreen\" stroke=\"black\" stroke-width=\"2\" points=\"155.167,-91.5 155.167,-145.5 218.462,-145.5 218.462,-91.5 155.167,-91.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"186.814\" y=\"-133.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"155.167,-127.5 218.462,-127.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"186.814\" y=\"-115.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"155.167,-109.5 218.462,-109.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"186.814\" y=\"-97.5\" font-family=\"Times,serif\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 4463805728forward&#45;&gt;4463806448forward -->\n",
       "<g id=\"edge8\" class=\"edge\"><title>4463805728forward&#45;&gt;4463806448forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M198.599,-145.722C202.465,-154.322 206.826,-164.025 210.942,-173.185\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"207.822,-174.78 215.113,-182.466 214.206,-171.91 207.822,-174.78\"/>\n",
       "</g>\n",
       "<!-- 4463806256forward -->\n",
       "<g id=\"node7\" class=\"node\"><title>4463806256forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M207.167,-273.5C207.167,-273.5 246.462,-273.5 246.462,-273.5 252.462,-273.5 258.462,-279.5 258.462,-285.5 258.462,-285.5 258.462,-315.5 258.462,-315.5 258.462,-321.5 252.462,-327.5 246.462,-327.5 246.462,-327.5 207.167,-327.5 207.167,-327.5 201.167,-327.5 195.167,-321.5 195.167,-315.5 195.167,-315.5 195.167,-285.5 195.167,-285.5 195.167,-279.5 201.167,-273.5 207.167,-273.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"226.814\" y=\"-315.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"195.167,-309.5 258.462,-309.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.814\" y=\"-297.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"195.167,-291.5 258.462,-291.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.814\" y=\"-279.5\" font-family=\"Times,serif\" font-size=\"10.00\">sigmoid</text>\n",
       "</g>\n",
       "<!-- 4463806304forward&#45;&gt;4463806448forward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>4463806304forward&#45;&gt;4463806448forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M255.735,-145.722C251.773,-154.322 247.303,-164.025 243.083,-173.185\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"239.813,-171.919 238.807,-182.466 246.17,-174.848 239.813,-171.919\"/>\n",
       "</g>\n",
       "<!-- 4463805824forward -->\n",
       "<g id=\"node9\" class=\"node\"><title>4463805824forward</title>\n",
       "<polygon fill=\"springgreen\" stroke=\"black\" stroke-width=\"2\" points=\"77.1667,-0.5 77.1667,-54.5 140.462,-54.5 140.462,-0.5 77.1667,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"108.814\" y=\"-42.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"77.1667,-36.5 140.462,-36.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"108.814\" y=\"-24.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"77.1667,-18.5 140.462,-18.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"108.814\" y=\"-6.5\" font-family=\"Times,serif\" font-size=\"10.00\">w1</text>\n",
       "</g>\n",
       "<!-- 4463805824forward&#45;&gt;4463806160forward -->\n",
       "<g id=\"edge11\" class=\"edge\"><title>4463805824forward&#45;&gt;4463806160forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M107.93,-54.7216C107.65,-63.0505 107.334,-72.4135 107.034,-81.3161\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"103.531,-81.3536 106.692,-91.4659 110.527,-81.5895 103.531,-81.3536\"/>\n",
       "</g>\n",
       "<!-- 4463806400forward&#45;&gt;4463806448forward -->\n",
       "<g id=\"edge7\" class=\"edge\"><title>4463806400forward&#45;&gt;4463806448forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M317.079,-142.651C302.663,-153.168 285.389,-165.769 269.83,-177.12\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"267.407,-174.555 261.391,-183.276 271.532,-180.21 267.407,-174.555\"/>\n",
       "</g>\n",
       "<!-- 4463805920forward -->\n",
       "<g id=\"node11\" class=\"node\"><title>4463805920forward</title>\n",
       "<polygon fill=\"springgreen\" stroke=\"black\" stroke-width=\"2\" points=\"395.167,-0.5 395.167,-54.5 458.462,-54.5 458.462,-0.5 395.167,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"426.814\" y=\"-42.5\" font-family=\"Times,serif\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"395.167,-36.5 458.462,-36.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"426.814\" y=\"-24.5\" font-family=\"Times,serif\" font-size=\"10.00\">value= 3.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"395.167,-18.5 458.462,-18.5 \"/>\n",
       "<text text-anchor=\"middle\" x=\"426.814\" y=\"-6.5\" font-family=\"Times,serif\" font-size=\"10.00\">w3</text>\n",
       "</g>\n",
       "<!-- 4463805920forward&#45;&gt;4463806400forward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>4463805920forward&#45;&gt;4463806400forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M403.833,-54.7216C395.82,-63.8653 386.714,-74.2553 378.249,-83.9142\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"375.589,-81.6384 371.631,-91.4659 380.854,-86.2522 375.589,-81.6384\"/>\n",
       "</g>\n",
       "<!-- 4463806448forward&#45;&gt;4463806256forward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>4463806448forward&#45;&gt;4463806256forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M226.814,-236.722C226.814,-245.051 226.814,-254.414 226.814,-263.316\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"223.314,-263.466 226.814,-273.466 230.314,-263.466 223.314,-263.466\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x10a1048b0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w1 = Scalar(1.0, label='w1')\n",
    "w2 = Scalar(2.0, label='w2')\n",
    "w3 = Scalar(3.0, label='w3')\n",
    "b = Scalar(4.0, label='b')\n",
    "x1 = Scalar(1.1, label='x1', requires_grad=False)\n",
    "x2 = Scalar(2.1, label='x2', requires_grad=False)\n",
    "x3 = Scalar(3.1, label='x3', requires_grad=False)\n",
    "h = scalar_sum([w1 * x1, w2 * x2, w3 * x3, b])\n",
    "y = h.sigmoid()\n",
    "draw_graph(y)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
